<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/8/31
 * Time: 14:31
 */

namespace app\controller\phone;


use think\facade\Db;

class Login
{
    public function login(){
        $post = input('post.');
        $error = '';
        if(!empty($post)){
            $username = $post['username'];
            $password = $post['password'];
            $user = Db::name('user')
                ->where('username',$username)
                ->find();
            if (empty($user['id'])){
                $error = '账号不存在';
            }else {
                $valid_username = $user['username'];
                $valid_password = $user['password'];
                // 检查用户名和密码是否正确
                if ($username === $valid_username && $password === $valid_password && $user['status'] == 1) {
                    session_start();
                    // 登录成功，设置session变量
                    $_SESSION['user_loggedin'] = true;
                    $_SESSION['username'] = $username;
                    $_SESSION['timeout'] = time() + 86400;
                    // 重定向到受保护的页面或显示欢迎信息
                    header('Location:/phone');
                    exit;
                } elseif ($user['status'] == 2) {
                    $error = '账号已经被禁用';
                } else {
                    // 登录失败，显示错误信息
                    $error = '用户名或密码错误';
                }
            }
        }
        return view('/phone/login',['error' => $error]);
    }
}